{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e089e84f-67f0-402b-9a17-7bc899090d7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cuda\n"
     ]
    }
   ],
   "source": [
    "from transformers import T5Tokenizer, T5ForConditionalGeneration\n",
    "\n",
    "import torch\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6fd5ccbf-5330-4e46-9df3-9a1635a0d415",
   "metadata": {},
   "outputs": [],
   "source": [
    "tokenizer = T5Tokenizer.from_pretrained('t5-base', model_max_length=200)\n",
    "model = T5ForConditionalGeneration.from_pretrained('t5-base', return_dict=True)\n",
    "model = model.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8b5a449e-e458-41e1-ae4e-b8d89ca333e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "language_sequence = (\"It’s such a beautiful morning today!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0f710e47-7dff-4f16-a1a3-f0fd89c403c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_ids = tokenizer(\"translate English to French: \" + language_sequence, return_tensors=\"pt\", truncation=True).input_ids.to(device)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6fd5a9a8-9212-49aa-9b64-0969c61f64d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "language_ids = model.generate(input_ids, max_new_tokens=200)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1188a72f-1798-4651-8516-0b09ac47dd2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "language_translation = tokenizer.decode(language_ids[0], skip_special_tokens=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "55104149-64af-4e24-a34b-cc0be43204cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C’est un matin si beau!\n"
     ]
    }
   ],
   "source": [
    "print(language_translation)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
